package ch.christianschweizer.apps.ereservation.ejb;

import java.util.Date;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import ch.christianschweizer.apps.ereservation.model.History;
import ch.christianschweizer.apps.ereservation.model.History.HistoryType;
import ch.christianschweizer.apps.ereservation.model.Inbox;

@Stateless
@LocalBean
@TransactionManagement(TransactionManagementType.CONTAINER)
public class InboxService extends AbstractService {

	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public long createInbox(Inbox inbox) {
		inbox = persist(inbox);

		History history = new History.HistoryBuilder().date(new Date())
				.type(HistoryType.CREATE).build();
		inbox.getHistory().add(history);

		merge(inbox);

		return inbox.getUniqueIdentifier();
	}

	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public Inbox findById(long id) {
		return findById(Inbox.class, id);
	}

}
